package com.tuo51.childcare.service.impl;

import com.tuo51.childcare.domain.dailyrecord.DailyGrowRecord;
import com.tuo51.childcare.domain.feed.Feeds;
import com.tuo51.childcare.repository.DailyGrowRecordRepository;
import com.tuo51.childcare.repository.FeedsRepository;
import com.tuo51.childcare.service.FeedsService;
import com.tuo51.core.domain.staffs.Staff;
import com.tuo51.core.service.StaffService;
import com.tuo51.evaluate.domain.childrens.Children;
import com.tuo51.evaluate.domain.evaluatingV2.EvaluateV2Record;
import com.tuo51.evaluate.repositories.children.ChildrenRepository;
import com.tuo51.evaluate.service.ChildrenService;
import com.tuo51.evaluate.service.EvaluateV2Service;
import com.tuo51.foudation.constant.CommonConstant;
import com.tuo51.foudation.exception.BusinessException;
import com.tuo51.foudation.model.ErrorCode;
import com.tuo51.foudation.query.base.PagerQuery;
import com.tuo51.observation.domain.ObservateRecord;
import com.tuo51.observation.service.ObservationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static com.tuo51.foudation.constant.FeedsConstant.*;

/**
 * 家长端
 */
@Service
public class FeedsServiceImpl implements FeedsService {

    @Autowired
    FeedsRepository feedsRepository;

    @Autowired
    ChildrenRepository childrenRepository;

    @Autowired
    EvaluateV2Service evaluateV2Service;

    @Autowired
    StaffService staffService;

    @Autowired
    private ObservationService observationService;

    @Autowired
    ChildrenService childrenService;

    @Autowired
    DailyGrowRecordRepository growRecordRepository;

    @Override
    public Page<Feeds> getAllByChildrens(String uid, String filters,  PagerQuery pagerQuery){
        PageRequest pageRequest = PageRequest.of(pagerQuery.getPageNum(), pagerQuery.getPageSize());

        if (StringUtils.isNotEmpty(filters)){
            String[] fs = filters.split("\\|");

            List<Integer> types = Arrays.asList(fs).stream().map(k->Integer.parseInt(k)).collect(Collectors.toList());

            Page<Feeds> feeds = feedsRepository.findAllByUidAndDeletedIsFalseAndTypeInOrderByCreatedDesc(uid, types, pageRequest);

            return feeds;
        }

        Page<Feeds> feeds = feedsRepository.findAllByUidAndDeletedIsFalseOrderByCreatedDesc(uid, pageRequest);

        return feeds;
    }

    @Override
    public Feeds getOne(String uid, String feedId) {
        Feeds feeds = feedsRepository.getByIdAndUidAndDeletedIsFalse(feedId, uid);
        if (feeds == null){
            throw new BusinessException(ErrorCode.FEEDS_NOT_EXIST, "动态不存在！");
        }
        return feeds;
    }

    @Override
    public Feeds getLatestFeeds(String uid, FeedsType type){
        Feeds feeds = feedsRepository.getTop1ByUidAndTypeAndDeletedIsFalseOrderByCreatedDesc(uid, type.getType());
        return feeds;
    }

    @Override
    public Object getDetailEvaluate(String linkId) {
        EvaluateV2Record result = evaluateV2Service.getEvaluateDetail(linkId, true);
        return result;
    }

    @Override
    public Object getDetailObservate(String linkId) {
        ObservateRecord result = observationService.getRecordById(linkId);
        return result;
    }

    @Override
    public Object getDetailGrowRecord(String linkId){
        DailyGrowRecord result = growRecordRepository.getOne(linkId);
        return result;
    }

    @Override
    public Object getLinkedObjects(String uid, Feeds f, PagerQuery pagerQuery){

        int value = f.getType();

        Object result = null;

        switch (value) {
            case EVALUATE_RECORD: {
                result = getDetailEvaluate(f.getLinkId());
            }
            break;
            case BIZRECORD_GROWUPENV: {

            }
            break;
            case PARENT_MEMO:{

            }
            break;
            case GROW_RECORD:{
                result = getDetailGrowRecord(f.getLinkId());
            }
            break;
            case DAILY_TASK:{

            }
            break;
            case DAILY_OBSERVE:{
                result = getDetailObservate(f.getLinkId());
            }
            break;
        }

        if (result == null){
            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "关联对象不存在");
        }

        return result;
    }

    @Override
    public List<Feeds> postFeeds(Children children, String title, String content, String resources, Integer mediaType, FeedsType type, String linkedId)
    {
        String guardianPhone = children.getExtendInfo().getGuardianPhone();
        if (StringUtils.isNotEmpty(guardianPhone)){
            List<Staff> parents = staffService.getByPhone(guardianPhone, null, CommonConstant.Platform.WXMP.codeOf());
            List<Feeds> ret = parents.stream().map(k->postFeeds(k, children, title, content, resources, mediaType, type, linkedId)).collect(Collectors.toList());
            return ret;
        }else{
            throw new BusinessException(ErrorCode.PARENT_PHONE_NEEDED, "家长电话没有填写");
        }
    }


    @Override
    public Feeds postFeeds(Staff parent, Children children, String title, String content, String resources, Integer mediaType, FeedsType type, String linkedId){
        if (children == null) {
            children = childrenService.defaultChildrenForParent(parent, false);
        }

        if(children == null){
            throw new BusinessException("没有指定宝宝");
        }

        String childrenId = children.getId();

        //创建familyFeed
        //TODO  这里需要链接对象

        //创建Feed
        Feeds f = new Feeds();
        f.setTitle(title);
        f.setContent(content);
        f.setUid(parent.getId());
        f.setChildrenId(childrenId);
        f.setChildrenName(children.getName());

        f.setTitle(title);
        f.setContent(content);

        if ( mediaType == null || mediaType == 0){
            f.setImages(resources);
        }else if(mediaType == 1){
            f.setVideos(resources);
        }

        f.setType(type.getType());
        f.setTypeVal(type.getTypeVal());
        f.setLinkId(linkedId);


        feedsRepository.save(f);

        return f;
    }
}
